import csv
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 新建一个 fliggy.csv 文件,w 表示是新创建一个文件, encoding 为 utf-8-sig，中文能正常显示,
# newline="" 不能省略，省略了之后，会换行
csvFileName="fliggy3.csv"
with open(csvFileName, "w", encoding='utf-8-sig', newline="") as f:
    csv_write = csv.writer(f)
    csv_head = ["名称", "月", "日", "价格", "余量"]
    csv_write.writerow(csv_head)

#接着就是配置 chromedriver
options = Options()
#options.add_argument('--headless')  # headless, 无头浏览器, 不显示用户界面
#options.add_argument('--disable-gpu')  # 不使用 gpu
#options.add_argument("--incognito")  # 以隐身模式打开
options.add_experimental_option("excludeSwitches", ['enable-automation'])
# prefs = {"profile.managed_default_content_settings.images": 2}  # 不显示图片
# options.add_experimental_option("prefs", prefs)
# chromedriver.exe 的路径
driver_path = r"D:\1MyFiles\code\python\python课件代码\class03\chromedriver_win32_96.0.4664.45\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path, options=options)


#CDP执行JavaScript 代码  重定义windows.navigator.webdriver的值
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
    Object.defineProperty(navigator, 'webdriver', {
        get: () => undefined
    })
    """
})

#url = 'https://baidu.com'
url = 'https://traveldetail.fliggy.com/item.htm?id=623874241606'
driver.get(url)
print(url)
time.sleep(60) #可能要登录


products=driver.find_elements_by_class_name('item-prop')  
for product in products:
    product.click()  #商品名
    time.sleep(2)
    months=driver.find_elements_by_class_name('month-tab')
    for month in months:
        month.click()  #月份
        time.sleep(2)
        state=-1 #判断是否是本月的日期
        cells=driver.find_elements_by_class_name('date-cell')  #日期
        for cell in cells:
            day=cell.find_element_by_class_name('num') 

            if state==-1 and day.text!='1':  #上个月的日期
                continue
            elif state==-1 and day.text=='1': #本月的一号
                state=0
            elif state==0 and day.text=='1': #下个月的一号
                state=1
                continue
            elif state==1:    #下个月的日期
                continue

            #if cell.get_attribute("class").index("disabled")!=-1:
            if "disabled" in cell.get_attribute("class"):
                continue
            
            
            price=cell.find_element_by_class_name('price')   
            numbers=cell.find_elements_by_class_name('quantity') 
            number='' 
            if len(numbers)!=0:
                number=numbers[0].text.lstrip('余').rstrip('件')
            row = [product.text, month.text, day.text, price.text, number]
            # encoding 为 utf-8-sig, 中文能正常显示, 不信的话, 换成 utf-8试试
            out = open(csvFileName, "a+", encoding='utf-8-sig', newline="")
            csv_writer = csv.writer(out, dialect="excel")
            csv_writer.writerow(row)
            out.close()   
    # 休息 3 秒接着处理
    time.sleep(1)

  

